<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

class FilePeer extends BaseFilePeer
{
	
	const PRIVATE_PERMISSION = 0;
	const GROUP_PERMISSION = 1;
	const NETWORK_PERMISSION = 2;
	const INTERNAL_PERMISSION = 3;
	const PUBLIC_PERMISSION = 4;

	/**
	 * retrieves a single File via the Code and the Owner(User) id
	 * Does a SELECT * FROM file WHERE code = :code and owner_id = :owner_id
	 * @param $code, the code ref via annotate
	 * @param $owner_id, the owner/user id
	 * @return File or null if not found
	 */
	public static function retrieveByCodeAndOwnerId($code, $owner_id) {
		$c = new Criteria();
		//$code_criterion = $c->getNewCriterion(FilePeer::CODE, $code, Criteria::EQUAL);
		//$owner_criterion = $c->getNewCriterion(FilePeer::OWNER_ID, $owner_id, Criteria::EQUAL);		
		// add to the Criteria
		$c->add(FilePeer::CODE, $code, Criteria::EQUAL);
		$c->add(FilePeer::OWNER_ID, $owner_id, Criteria::EQUAL);
		
		// perform the select query and return a single file object if it exists
		return FilePeer::doSelectOne($c);
	}

	/**
	 * retrieves all Files via Group id
	 * Does a SELECT * FROM file WHERE group_id = :group_id
	 * @param $goup_id, the group
	 * @return set of File or null if not found
	 */
	public static function retrieveByGroupId($group_id) {
		$c = new Criteria();
		$group_criterion = $c->getNewCriterion(FilePeer::GROUP_ID, $group_id, Criteria::EQUAL);		
		// add to the Criteria
		$c->add($group_criterion);
		
		// perform the select query and return a file objects
		return FilePeer::doSelect($c);
	}

	/**
	 * retrieves all Files via Owner(User) id
	 * Does a SELECT * FROM file WHERE owner_id = :owner_id
	 * @param $owner_id, the owner/user id
	 * @return set of File or null if not found
	 */
	public static function retrieveByOwnerId($owner_id) {
		$c = new Criteria();
		$owner_criterion = $c->getNewCriterion(FilePeer::OWNER_ID, $owner_id, Criteria::EQUAL);		
		// add to the Criteria
		$c->add($owner_criterion);
		
		// perform the select query and return a file objects
		return FilePeer::doSelect($c);
	}

	/**
	 * retrieves all Tags of Files via Owner(User) id
	 *
	 * @param $owner_id, the owner/user id
	 * @return a key sorted (alpha) array (hashmap) of keywords and the number of times the tag has occured
	 */
	public static function retrieveTagsByOwnerId($owner_id) {
		$files = FilePeer::retrieveByOwnerId($owner_id);
		$tags = array();
		foreach($files as $file) {
			if($file->getTags()) {
				$keys = explode(',',$file->getTags()); // assume it is comma delimited
				foreach($keys as $key) {
					$k = strtolower(trim($key));
					if(array_key_exists($k,$tags)) {
						$tags[$k] = $tags[$k] + 1;
					} else {
						$tags[$k] = 1;
					}
				}
			}
		}
		ksort($tags);
		return $tags;
		
	}
	
	/**
	 * Method to DELETE all rows from the file table.
	 * Overrides the BaseFilePeer doDeleteAll
	 *
	 * @return     int The number of affected rows (if supported by underlying database driver).
	 * @see	   BaseFilePeer::doDeleteAll()
	 */
	public static function doDeleteAll($con = null)
	{
		if (file_exists($index = Search::getLuceneIndexFile(Search::FILE))) {
    			sfToolkit::clearDirectory($index);
    			rmdir($index);
  		}
		return parent::doDeleteAll($con);
	}
	
	/**
	 * searches files by userid AND a tag
	 *
  	 * @param $user_id the user id
    	 * @param $tag the tag keyword
	 * @return	set of search hits
  	 */
	public static function searchFilesByUserIDAndTag($user_id, $tag) {
		$hits = Search::getForLuceneQuery(Search::GROUP,"userid:".$user_id." AND tags:".$tag);
		return $hits;
	}

	/**
	 * searches files by userid AND a tag.  returns an array of retrieved File objects
	 *
  	 * @param $user_id the user id
    	 * @param $tag the tag keyword
	 * @return	array of retrieved File objects
  	 */
	public static function searchFilesByUserIDAndTagReturnFileObjs($user_id, $tag) {
		$hits = FilePeer::searchFilesByUserIDAndTag($user_id,$tag);
		$files = array();
		foreach($hits as $hit) {
			$files[] = FilePeer::retrieveByPK($hit->pk);
		}
		return $files;
	}
	
	
	//retrieve by user id and the day it was created as a timestamp
	public static function retrieveByUserIdAndDate($user_id, $time_stamp)
	{
		$c = new Criteria();
		$c->add(FilePeer::OWNER_ID, $user_id);
		$c->add(FilePeer::CREATED_AT, date('Y-m-d', $time_stamp), Criteria::GREATER_EQUAL);
		$c->addAnd(FilePeer::CREATED_AT, date('Y-m-d', $time_stamp + 60*60*24), Criteria::LESS_THAN);
			
 		return FilePeer::doSelect($c); 		
	
	}
	
	public static function retrieveAllPublicByCreatedAt($time_stamp) {
		$c = new Criteria();
		$c->add(NotePeer::VISIBILITY, NotePeer::EVERYONE);
		$c->add(FilePeer::CREATED_AT, date('Y-m-d', $time_stamp), Criteria::GREATER_EQUAL);
		$c->addAnd(FilePeer::CREATED_AT, date('Y-m-d', $time_stamp + 60*60*24), Criteria::LESS_THAN);
			
 		return FilePeer::doSelect($c); 		
	}
	
	public static function retrieveById($id)
	{
		
		return FilePeer::retrieveByPK($id);
	}
}

